﻿/// <reference name="MicrosoftAjax.js"/>
/// <reference name="Dimok.Web.UI.JS.JQuery.VSDoc.js" assembly="Dimok.Web.UI"/>
/// <reference name="Dimok.Web.UI.JS.MSAjaxExtentions.js" assembly="Dimok.Web.UI"/>
/// <reference name="Dimok.Web.UI.JS.ajaxfileupload.js" assembly="Dimok.Web.UI"/>
/// <reference path="/../JS/UI/jquery.extentions.js" />
Type.registerNamespace("motobankcorp");
motobankcorp.UploadInfos = function(element) {
  motobankcorp.UploadInfos.initializeBase(this, [element]);
  Dimok.AccessorizeType(this);
  this._clearAccountsHandler = Function.createDelegate(this,this._clearAccounts);
}
motobankcorp.UploadInfos.prototype = {
  _interestTypeList: null,
  _paymentTypeList: null,
  _uploadButton: null,
  _fileUpload: null,
  _wireEvents: function() {
    this.get_RGE().add_rowDeselected(function(sender, eventArgs) {
      Dimok.FindPageEvents().raise_uploadInfoDeselected(eventArgs);
    });
    this.get_RGE().add_rowSelected(function(sender, eventArgs) {
      Dimok.FindPageEvents().raise_uploadInfoSelected(eventArgs);
    });
    this.get_RGE().add_rowContextMenuShowing(Function.createDelegate(this, this._onRowContextMenuShowing));
    this.get_RGE().add_rowContextMenuItemClicked(this._clearAccountsHandler);
    this.addHandlers(this.get_uploadButton(), { 'click': this._onFileUpload }, this);
  },
  _clearAccounts: function(sender, eventArgs) {
    if (eventArgs._item.get_value() != "ClearAccounts") return;
    var batchID = sender.get_batchID();
    if (!confirm("Remove all accounts from batch " + batchID)) return;
    var row = Dimok.Ajax({ methodName: "ClearUploadAccounts", wsObject: WSA }, { BatchID: batchID });
    if (row) {
      this.get_RGE().bindRow(row, this.get_RGE().get_contextMenuGridDataItem());
      this.get_RGE().get_grid().selectRow(this.get_RGE().get_contextMenuGridDataItem());
    }
  },
  _onFileUpload: function(eventArgs) {
    //if (!parseInt(this.get_interestTypeList().value)) return alert("Select Interest Type first.");
    //if (!parseInt(this.get_paymentTypeList().value)) return alert("Select Payment Type first.");
    $J.ajaxFileUpload({
      fileElementId: this.get_fileUpload().id,
      hiddens: { hdnUploadInfoID: eventArgs.target.get_batchID(),
        hdnInterestTypeID: this.get_interestTypeList().value,
        hdnPaymentsPerYear: this.get_paymentTypeList().value
      },
      dataType: "html",
      success: Function.createDelegate(this, this._onUploadSuccess),
      url: "/Manager/BayQuest/UploadBatch.aspx"
    });
    if (!this.set_fileUpload($J(eventArgs.target).parent().find("input:file")[0]))
      alert("File Upload element is not found.\nYou won't be able to upload more file.");
  },
  _onUploadSuccess: function(data) {
    $J.bubbleTip({ html: data, element: this.get_RGE().get_element(), flash: true });
    this.get_interestTypeList().value = "0";
    this.get_paymentTypeList().value = "0";
    this.rebind();
  },
  _onRowContextMenuShowing: function(sender, eventArgs) {
    var id = Dimok.$A(Dimok.$A(eventArgs, true).get_dataKeyValues(), true).get_ID();
    Dimok.AddProperty(sender, "batchID", id);
    Dimok.AddProperty(this.get_uploadButton(), "batchID", id);
  },
  initialize: function() {
    motobankcorp.UploadInfos.callBaseMethod(this, 'initialize');
    Sys.Application.add_load(Function.createDelegate(this, function() {
      this._wireEvents();
    }));
  },
  dispose: function() {
    this.get_RGE().remove_rowContextMenuItemClicked(this._clearAccountsHandler);
    motobankcorp.UploadInfos.callBaseMethod(this, 'dispose');
  }
}
motobankcorp.UploadInfos.registerClass('motobankcorp.UploadInfos', Dimok.Table);
